package sg.edu.nus.iss.team2s.jass.gui;

import java.awt.Frame;
import java.awt.Rectangle;

import sg.edu.nus.iss.team2s.jass.dto.Company;
import sg.edu.nus.iss.team2s.jass.dto.User;
import sg.edu.nus.iss.team2s.jass.dto.Advertiser;
import sg.edu.nus.iss.team2s.jass.dto.JobAdvert;
import sg.edu.nus.iss.team2s.jass.control.*;

import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Label;
import java.awt.TextField;
import java.awt.Choice;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import sg.edu.nus.iss.team2s.jass.util.*;
/**
 * <p>The <strong>JobAdvertDetailScreen</strong> is to show the details of jobAdvert and to maintain the jobAdvert. 
 * This class implements the functionality of create,delete,update,withdraw and apply.</p>
 * 
 * <p>To create new advertisement, no need to pass in jobadvertId during initialization. 
 *    To update/apply the existing job advent records, need to pass in jobadvertId during initialization.</p>
 * <p>Based the role of the login user,the different buttons will display. the cancel button is also visible and click to close the screen. 
 * Job Seeker will see the apply button. click to show the jobadvert response screen ({@link sg.edu.nus.iss.team2s.jass.gui.JobAdvertResponseScreen}).
 * Job Advertiser will see the save button ,withdraw button and delete button based certain conditions.
 * </p>
 * @author Lu Zhixian,Nie Xiaoyi
 * @version 1.1
 *
 */

public class JobAdvertDetailScreen extends Frame  {

	private static final long serialVersionUID = 1L;
	private Label lbl_jobTitle = null;
	private TextField txt_jobTitle = null;
	private Label lbl_jobDesc = null;
	private Choice cbo_company = null;
	private TextField txt_company = null;
	private TextField txt_jobDesc = null;
	private Label lbl_company = null;
	private Label lbl_experience = null;
	private TextField txt_experience = null;
	private Label lbl_techSkillList = null;
	private TextField txt_techSkillList = null;
	private Label lbl_manageSkillList = null;
	private TextField txt_manageSkillList = null;
	private Label lbl_salaryRange = null;

	private Label lbl_startDate = null;
	private TextField txt_startDate = null;
	private Label lbl_Location = null;
	private TextField txt_Location = null;
	private Choice cbo_salaryRange = null;
	private Label lbl_department = null;
	private TextField txt_department = null;
	private Label lbl_postDate = null;
	private TextField txt_postDate = null;
	private Label lbl_updateDate = null;
	private TextField txt_updateDate = null;
	private Label lbl_result = null;
	private Label lbl_status = null;
	private Label lbl_date1=null;
	private Label lbl_date2=null;
	
	private Button cmd_save = null;
	private Button cmd_delete = null;
	private Button cmd_withdraw = null;
	private Button cmd_apply = null;
	private Button cmd_cancel = null;
	

	private JobAdvert jobAdvert = null;
	private Advertiser advertiser=null;
	private boolean isNew=true;
	private boolean isEdit=false;
	/**
	 * Constructor of jobAvert
	 *
	 */
	/**
	 * This is the default constructor
	 * It is for job advertiser to create new jobadvert.
	 * So it assume the job advertiser is already login otherwise it will show error.
	 * 
	 * @param null
	 * @return a new JobAdvertDetailScreen instance
	 * @exception  DAOException due to retrieve data error from the database.
	 *
	 */
	public JobAdvertDetailScreen() {
		super();
		isNew=true;
		
		initialize();
		
		jobAdvert=new JobAdvert();
		
		User u=MainController.getUser();
		
		
		if (u!=null){
		 if (u.getUserType().equalsIgnoreCase(String.valueOf(User.JOBADVERTISER))){
			 cmd_save.setVisible(true);
		   }}
		
		txt_company.setVisible(false);
		cbo_company.setVisible(true);
		populateCompany();
		
	}
	/**
	 * Constructor of jobAvert
	 * 
	 * It is for job advertiser to update existing jobadvert if logined user is job advertiser.
	 * It is for job seeker to apply existing jobadvert if logined user is job seeker.
	 * Different button will show based the login user role and the status of jobadvert.
	 * 
	 * @param jobAdvertId the unique key of jobAdvert
	 * @return a new JobAdvertDetailScreen instance with respective buttons
	 * @exception  DAOException due to retrieve data error from the database.
	 */
	public JobAdvertDetailScreen(String jobAdvertId) {
		super();
		
		isNew=false;
		initialize();
		
		try {
			jobAdvert=JobAdvertController.getJobAdvert(jobAdvertId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			CommonUtility.showDialog(this, "Job Advert Retrieve Error", "Error: Cannot find the specifical job advert record!");
			e.printStackTrace();
			return;
		}
		
		User u=MainController.getUser();
	
		if (u==null){
			//not login		
		}
		else if(u.getUserType().equalsIgnoreCase(String.valueOf(User.JOBADVERTISER))){
			if (advertiser== null){
	
				try{
			    	advertiser=JobAdvertController.getAdvertiser(u.getLoginName());
					
		}catch(Exception exp){
			CommonUtility.showDialog(this, "Advertiser Retrieve Error", "Error: Cannot find the specifical advertiser record!");
			exp.printStackTrace();
			return;
		}
			}
			
		  if (jobAdvert.getAdvertiserId().equalsIgnoreCase(advertiser.getAdvertiserId())){
			  isEdit=true;
			cmd_delete.setVisible(true);
			cmd_withdraw.setVisible(true);
			cmd_save.setVisible(true);}
		}
		else if (u.getUserType().equalsIgnoreCase(String.valueOf(User.JOBSEEKER))){
			cmd_apply.setVisible(true);
		}
		
		if (jobAdvert.getStatus()==JobAdvert.WITHDRAW){
			  isEdit=false;
				lbl_result.setText("The Job Advertiserment was withdrawed.");
				lbl_status.setText("WITHDRAWED");
		     }
		else if (jobAdvert.getStatus()==JobAdvert.ACTIVE){
					lbl_status.setText("ACTIVE");	
				
		      }
		else if (jobAdvert.getStatus()==JobAdvert.DELETED){
			lbl_status.setText("Deleted");
			  isEdit=false;
			}
		
		cmd_delete.setVisible(isEdit);
		cmd_withdraw.setVisible(isEdit);
		cmd_save.setVisible(isEdit);
		
		if (isEdit){
			txt_company.setVisible(false);
			cbo_company.setVisible(true);
			populateCompany();
		}
		else{
			txt_company.setVisible(true);
			cbo_company.setVisible(false);
		}
		
		populateJobDetail();
		
	}
	/**
	 * This method initializes company information.
	 * Load Company information based on current login jobadvertiser.
	 * Only valid in the job advertiser edit mode.
     * @param null
	 * @return void
	 * @exception  DAOException due to retrieve data error from the database.
	 *
	 */
	private void populateCompany(){
		
		try
		{
			if (advertiser== null){
				User u=MainController.getUser();
				
			    if (u==null){
			    	CommonUtility.showDialog(this,  "User Record","Error: Cannot find the login user record!");
				
					return;	
			    }
			    else{
			    	advertiser=JobAdvertController.getAdvertiser(u.getLoginName());
			    }

			}		
		}catch(Exception exp){
			CommonUtility.showDialog(this, "Advertiser Retrieve Error", "Error: Cannot find the specifical advertiser record!");
			exp.printStackTrace();
			return;
		}
		
		try
		{
			List<Object> list=JobAdvertController.getCompanyList(advertiser.getAdvertiserId());
			Iterator<Object> iterator = list.iterator();
			Company obj;
			while(iterator.hasNext()){
				obj=(Company)iterator.next();
				cbo_company.add(obj.getName() +"-" +obj.getCompanyId());
			
			}
		}
		catch(Exception exp){
			CommonUtility.showDialog(this, "Load Company List", "Error: cannot load company list!");
			exp.printStackTrace();
			return;
			
		}

	}
	/**
	 * This method shows the specified jobAdvert information.
	 * @param null
	 * @return void
	 * @exception  DAOException due to retrieve data error from the database.
	 *
	 */
	private void populateJobDetail(){
		
		txt_jobTitle.setText(jobAdvert.getJobTitle());
		txt_jobDesc.setText(jobAdvert.getJobDesc());
		txt_experience.setText(jobAdvert.getExperience());
		txt_techSkillList.setText(jobAdvert.getTechSkillList());
		txt_manageSkillList.setText(jobAdvert.getManageSkillList());
		txt_startDate.setText(CommonUtility.parseFromDate(jobAdvert.getStartDate(), "yyyy-MM-dd"));
		txt_department.setText(jobAdvert.getDepartment());
		txt_updateDate.setText(CommonUtility.parseFromDate(jobAdvert.getUpdateDate(),"yyyy-MM-dd"));
		txt_Location.setText(jobAdvert.getLocation());
		txt_postDate.setText(CommonUtility.parseFromDate(jobAdvert.getPostDate(),"yyyy-MM-dd"));
		
		Company objComp;
		try
		{
			objComp=JobAdvertController.getCompany(jobAdvert.getCompanyId());
		}
		catch(Exception exp){
			CommonUtility.showDialog(this, "Load Company Detail", "Error: cannot load company detail!");
			exp.printStackTrace();
			return;	
			
		}

		if (isEdit){
			cbo_company.select(objComp.getName()+"-" +objComp.getCompanyId());	
		}else{
			if (objComp.getShowName().equalsIgnoreCase("yes"))
			{txt_company.setText(objComp.getName());}	
			else{
				txt_company.setText("An Industry Leading Company");
			}
		}
		
		cbo_salaryRange.select(jobAdvert.getSalaryRange());
		
	}

	/**
	 * This method initializes layout.
	 * 
     * @param null
	 * @return void
	 * @exception null
	 */
	private void initialize() {
		lbl_department = new Label();
		lbl_department.setBounds(new Rectangle(30, 482, 70, 27));
		lbl_department.setText("Department:");
		lbl_Location = new Label();
		lbl_Location.setBounds(new Rectangle(30, 399, 78, 27));
		lbl_Location.setText("Location:");
		lbl_startDate = new Label();
		lbl_startDate.setBounds(new Rectangle(30, 347, 76,27));
		lbl_startDate.setText("Start Date:");
		lbl_date1 = new Label();
		lbl_date1.setText("yyyy-MM-dd");
		lbl_date1.setBounds(new Rectangle(330, 347, 70, 27));
		lbl_salaryRange = new Label();
		lbl_salaryRange.setBounds(new Rectangle(30, 310, 81, 27));
		lbl_salaryRange.setText("Salary Range:");
		lbl_manageSkillList = new Label();
		lbl_manageSkillList.setBounds(new Rectangle(30, 216, 81, 27));
		lbl_manageSkillList.setText("Manage Skills:");
		lbl_techSkillList = new Label();
		lbl_techSkillList.setBounds(new Rectangle(30, 170, 78, 27));
		lbl_techSkillList.setText("Tech Skills:");
		lbl_experience = new Label();
		lbl_experience.setBounds(new Rectangle(30, 260, 70, 27));
		lbl_experience.setText("Experience:");
		lbl_company = new Label();
		lbl_company.setBounds(new Rectangle(30, 436, 64, 27));
		lbl_company.setText("Company:");
		lbl_jobDesc = new Label();
		lbl_jobDesc.setBounds(new Rectangle(30, 119, 65, 27));
		lbl_jobDesc.setText("Job Desc:");
		lbl_jobTitle = new Label();
		lbl_jobTitle.setBounds(new Rectangle(30, 64, 64, 27));
		lbl_jobTitle.setText("Job Title:");
		lbl_status = new Label();
		lbl_status.setBounds(new Rectangle(600, 64, 100, 27));
		lbl_status.setText("STATUS");
		//lbl_status.setForeground(Color.RED);
		
		
		lbl_postDate = new Label();
		lbl_postDate.setText("Post Date:");
		lbl_postDate.setBounds(new Rectangle(30, 520, 70, 27));
		
	
		lbl_date2 = new Label();
		lbl_date2.setText("yyyy-MM-dd");
		lbl_date2.setBounds(new Rectangle(330, 520, 70, 27));
		
		lbl_updateDate = new Label();
		lbl_updateDate.setBounds(new Rectangle(30, 555, 70, 27));
		lbl_updateDate.setText("Update Date:");
		
		lbl_result = new Label();
		lbl_result.setBounds(new Rectangle(100, 660, 600, 50));
		lbl_result.setText("");
		lbl_result.setForeground(Color.red);
		//lbl_result.setBackground(Color.gray);
		
		this.setLayout(null);
		this.setSize(773, 750);
		this.setTitle("Jass - Job Details Screen");

		this.add(lbl_jobTitle, null);
		this.add(getTxt_jobTitle(), null);
		this.add(lbl_result,null);
		this.add(lbl_jobDesc, null);
		this.add(getCbo_company(), null);	
		this.add(getTxt_company(), null);
		
		
		this.add(getTxt_jobDesc(), null);
		this.add(lbl_company, null);
		this.add(lbl_experience, null);
		this.add(getTxt_experience(), null);
		this.add(lbl_techSkillList, null);
		this.add(getTxt_techSkillList(), null);
		this.add(lbl_manageSkillList, null);
		this.add(getTxt_manageSkillList(), null);
		this.add(lbl_salaryRange, null);
//		this.add(label, null);
		this.add(lbl_startDate, null);
		this.add(lbl_date1, null);
		this.add(getTxt_startDate(), null);
		this.add(lbl_Location, null);
		this.add(getTxt_Location(), null);
		this.add(getCbo_salaryRange(), null);
		this.add(lbl_department, null);
		this.add(getTxt_department(), null);
		this.add(lbl_postDate, null);
		this.add(lbl_date2,null);
		this.add(lbl_updateDate, null);
		this.add(getTxt_updateDate(), null);
		this.add(getTxt_postDate(),null);
		this.add(getCmd_save(), null);
		this.add(getCmd_delete(), null);
		this.add(getCmd_apply(), null);
		this.add(getCmd_withdraw(), null);
		this.add(getCmd_cancel(), null);
		
		cmd_delete.setVisible(false);
		cmd_withdraw.setVisible(false);
		cmd_save.setVisible(false);
		cmd_apply.setVisible(false);
		
		CommonUtility.disposeWhenWindowClosing(this);
		CommonUtility.alignScreenCenter(this);
	}

	/**
	 * This method initializes txt_jobTitle	
	 * 	
	 * @param null
	 * @return java.awt.TextField	
	 */
	private TextField getTxt_jobTitle() {
		if (txt_jobTitle == null) {
			txt_jobTitle = new TextField();
			txt_jobTitle.setBounds(new Rectangle(122, 64, 300, 27));
		}
		return txt_jobTitle;
	}

	/**
	 * This method initializes cbo_company	
	 * @param null	
	 * @return java.awt.Choice	
	 */
	private Choice getCbo_company() {
		if (cbo_company == null) {
			cbo_company = new Choice();
			cbo_company.setBounds(new Rectangle(122, 445, 300, 30));
		}
		return cbo_company;
	}

	/**
	 * This method initializes txt_jobDesc	
	 * @param null 	
	 * @return java.awt.TextField	
	 */
	private TextField getTxt_jobDesc() {
		if (txt_jobDesc == null) {
			txt_jobDesc = new TextField();
			txt_jobDesc.setBounds(new Rectangle(122, 117, 590, 37));
		}
		return txt_jobDesc;
	}

	/**
	 * This method initializes txt_experience	
	 * @param null 
	 * @return java.awt.TextField	
	 */
	private TextField getTxt_experience() {
		if (txt_experience == null) {
			txt_experience = new TextField();
			txt_experience.setBounds(new Rectangle(122, 260, 590, 37));
		}
		return txt_experience;
	}

	/**
	 * This method initializes txt_techSkillList	
	 * @param null 
	 * @return java.awt.TextField	
	 */
	private TextField getTxt_techSkillList() {
		if (txt_techSkillList == null) {
			txt_techSkillList = new TextField();
			txt_techSkillList.setBounds(new Rectangle(122, 164, 590, 37));
		}
		return txt_techSkillList;
	}

	/**
	 * This method initializes txt_manageSkillList	
	 * @param null 
	 * @return java.awt.TextField	
	 */
	private TextField getTxt_manageSkillList() {
		if (txt_manageSkillList == null) {
			txt_manageSkillList = new TextField();
			txt_manageSkillList.setBounds(new Rectangle(122, 215, 590, 37));
		}
		return txt_manageSkillList;
	}

	/**
	 * This method initializes txt_startDate	
	 * @param null 
	 * @return java.awt.TextField	
	 */
	private TextField getTxt_startDate() {
		if (txt_startDate == null) {
			txt_startDate = new TextField();
			txt_startDate.setBounds(new Rectangle(122, 354, 200, 27));
			txt_startDate.setText(CommonUtility.parseFromDate(new java.util.Date(), "yyyy-MM-dd"));
		}
		return txt_startDate;
	}

	/**
	 * This method initializes txt_Location	
	 * @param null 
	 * @return java.awt.TextField	
	 */
	private TextField getTxt_Location() {
		if (txt_Location == null) {
			txt_Location = new TextField();
			txt_Location.setBounds(new Rectangle(122, 398, 200, 27));
		}
		return txt_Location;
	}

	/**
	 * This method initializes cbo_salaryRange	
	 * @param null 
	 * @return java.awt.Choice	
	 */
	private Choice getCbo_salaryRange() {
		if (cbo_salaryRange == null) {
			cbo_salaryRange = new Choice();
			cbo_salaryRange.setBounds(new Rectangle(122, 310, 300, 30));
			cbo_salaryRange.add("<1000");
			cbo_salaryRange.add("1001-2000");
			cbo_salaryRange.add("2001-3000");
			cbo_salaryRange.add("3001-4000");
		//	cbo_salaryRange.add("4001-5000");
			cbo_salaryRange.add("4001-5000");
			cbo_salaryRange.add(">5000");
		}
		return cbo_salaryRange;
	}

	/**
	 * This method initializes txt_company	
	 * @param null 
	 * @return java.awt.TextField	
	 */
	private TextField getTxt_company() {
		if (txt_company == null) {
			txt_company = new TextField();
			txt_company.setBounds(new Rectangle(122, 445, 300, 27));
		}
		return txt_company;
	}
	
	/**
	 * This method initializes txt_department	
	 * @param null 
	 * @return java.awt.TextField	
	 */
	private TextField getTxt_department() {
		if (txt_department == null) {
			txt_department = new TextField();
			txt_department.setBounds(new Rectangle(122, 485, 300, 27));
		}
		return txt_department;
	}
	/**
	 * This method initializes txt_updateDate.
	 * @param null 
	 * @return java.awt.TextField
	 */
	private TextField getTxt_updateDate() {
		if (txt_updateDate == null) {
			txt_updateDate = new TextField();
			txt_updateDate.setBounds(new Rectangle(122, 555, 200, 27));
			txt_updateDate.setText(CommonUtility.parseFromDate(new java.util.Date(), "yyyy-MM-dd"));
			txt_updateDate.setEditable(false);
		}
		return txt_updateDate;
	}
	/**
	 * This method initializes txt_postDate.
	 * @param null 
	 * @return java.awt.TextField
	 */
	private TextField getTxt_postDate() {
		if (txt_postDate == null) {
			txt_postDate = new TextField();
			txt_postDate.setText(CommonUtility.parseFromDate(new java.util.Date(), "yyyy-MM-dd"));
			txt_postDate.setBounds(new Rectangle(122, 520, 200, 27));
		}
		return txt_postDate;
	}
	/**
	 * This method initializes cmd_save.
     * @param null 
	 * @return java.awt.Button
	 */
	private Button getCmd_save(){
		if (cmd_save == null) {
			cmd_save = new Button("Save");
			cmd_save.setFont(new Font(null, Font.BOLD, 15));
//			cmd_apply.setBackground(java.awt.Color.BLUE);
			cmd_save.setBounds(new Rectangle(100, 600, 110, 29));
			cmd_save.addActionListener(new ActionListener(){

				@Override
				public void actionPerformed(ActionEvent e) {
						try {
							save();
						} catch (Exception e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
							CommonUtility.showDialog(JobAdvertDetailScreen.this, "Job Advert Save Error",e1.getMessage());
						}
					}				
			});
		}
		return cmd_save;
	}
	/**
	 * This method initializes cmd_delete.
	 * @param null 
	 * @return java.awt.Button
	 */
	private Button getCmd_delete(){
		if (cmd_delete == null) {
			cmd_delete = new Button("Delete");
			cmd_delete.setFont(new Font(null, Font.BOLD, 15));
//			cmd_apply.setBackground(java.awt.Color.BLUE);
			cmd_delete.setBounds(new Rectangle(240, 600, 110, 29));
			cmd_delete.addActionListener(new ActionListener(){

				@Override
				public void actionPerformed(ActionEvent e) {
						try {
	        String msg = "Do you really want to delete this job advert : " + jobAdvert.getJobTitle() + " ?";
							
							ConfirmDialog d = new ConfirmDialog (JobAdvertDetailScreen.this,"Confirm deletion", msg ){
								private static final long serialVersionUID = 1L;
								protected boolean performOkAction() {
									delete();
									return true;
								}
							};
							d.pack();
							d.setVisible(true);
							
						} catch (Exception e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
							CommonUtility.showDialog(JobAdvertDetailScreen.this, "Job Advert delete Error", e1.getMessage());
						}
					}				
			});
		}
		return cmd_delete;
	}
	/**
	 * This method initializes cmd_withdraw.
	 * @param null 
	 * @return java.awt.Button
	 */
	private Button getCmd_withdraw(){
		if (cmd_withdraw == null) {
			cmd_withdraw = new Button("Withdraw");
			cmd_withdraw.setFont(new Font(null, Font.BOLD, 15));
//			cmd_apply.setBackground(java.awt.Color.BLUE);
			cmd_withdraw.setBounds(new Rectangle(370, 600, 110, 29));
			cmd_withdraw.addActionListener(new ActionListener(){

				@Override
				public void actionPerformed(ActionEvent e) {
						try {
							String msg = "Do you really want to withdraw this job advert : " + jobAdvert.getJobTitle() + " ?";
							
							ConfirmDialog d = new ConfirmDialog (JobAdvertDetailScreen.this,"Confirm withdraw", msg ){
								private static final long serialVersionUID = 1L;
								protected boolean performOkAction() {
									withdraw();
									return true;
								}
							};
							d.pack();
							d.setVisible(true);
							
							
						} catch (Exception e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
							CommonUtility.showDialog(JobAdvertDetailScreen.this, "Job Advert withdraw Error", e1.getMessage());
						}
					}				
			});
		}
		return cmd_withdraw;
	}
	/**
	 * This method initializes cmd_apply.
	 * @param null 
	 * @return java.awt.Button
	 */
	private Button getCmd_apply(){
		if (cmd_apply  == null) {
			cmd_apply  = new Button("Apply");
			cmd_apply.setFont(new Font(null, Font.BOLD, 15));
//			cmd_apply.setBackground(java.awt.Color.BLUE);
			cmd_apply.setBounds(new Rectangle(370, 600, 110, 29));
			cmd_apply.addActionListener(new ActionListener(){

				@Override
				public void actionPerformed(ActionEvent e) {
						try {
							applyJob();
						} catch (Exception e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
							CommonUtility.showDialog(JobAdvertDetailScreen.this,  "Job Advert Apply Error",e1.getMessage());
						}
					}				
			});
		}
		return cmd_apply;
	}
	/**
	 * This method initializes cmd_cancel.
	 * @param null 
	 * @return java.awt.Button
	 */
	private Button getCmd_cancel(){
		if (cmd_cancel  == null) {
			cmd_cancel = new Button("Cancel");
			cmd_cancel.setFont(new Font(null, Font.BOLD, 15));
//			cmd_apply.setBackground(java.awt.Color.BLUE);
			cmd_cancel.setBounds(new Rectangle(600, 600, 110, 29));
			cmd_cancel.addActionListener(new ActionListener(){

				@Override
				public void actionPerformed(ActionEvent e) {
						try {
							cancel();
						} catch (Exception e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
							CommonUtility.showDialog(JobAdvertDetailScreen.this, "Job Advert Cancel Error" , e1.getMessage());
						}
					}				
			});
		}
		return cmd_cancel;
	}
	/**
	 * This method is to validate whether the value of textbxo is empty.
	 * @param txt the specified textbox control.
	 * @return boolean
	 */
	private boolean txtIsEmpty(TextField txt){
		String s=txt.getText();
		
		if( s==null)
			return false;
		
		if (s.trim().equalsIgnoreCase(""))
			return false;
		
		return true;
		
	}
	/**
	 * This method is to validate the user input value. 
	 * This method also assign the values in the screens controls to the jobadvert object.
	 * @param null 
	 * @return boolean. if pass validation, return true else return false
	 */
	private boolean validateCommon(){
		if (txtIsEmpty(txt_jobTitle)==false)
			return false;
		
		if (txtIsEmpty(txt_jobDesc)==false)
			return false;
		
		
		if (txtIsEmpty(txt_startDate)==false)
			return false;
		
		//Assign values;
		
		jobAdvert.setJobTitle(txt_jobTitle.getText());
		jobAdvert.setJobDesc(txt_jobDesc.getText());
		jobAdvert.setExperience(txt_experience.getText());
		jobAdvert.setTechSkillList(txt_techSkillList.getText());
		jobAdvert.setManageSkillList(txt_manageSkillList.getText());
		jobAdvert.setDepartment(txt_department.getText());
		jobAdvert.setLocation(txt_Location.getText());
		
		try
		{
			jobAdvert.setStartDate(CommonUtility.parseToDate(txt_startDate.getText(), "yyyy-MM-dd"));
			jobAdvert.setUpdateDate(new Date());
			jobAdvert.setPostDate(CommonUtility.parseToDate(txt_postDate.getText(),"yyyy-MM-dd"));
		}
	    catch(Exception exp){
	    	exp.printStackTrace();
			CommonUtility.showDialog(this, "Date Input Error:yyyy-MM-dd " ,exp.getMessage());	
	    }
	    jobAdvert.setSalaryRange(cbo_salaryRange.getSelectedItem());
	    
	    String company=cbo_company.getSelectedItem();
	    
	    int i=company.lastIndexOf("-");
	    
	    jobAdvert.setCompanyId(company.substring(i+1));
	    
	    //set advertiserId
	
	    if (isNew)
	    jobAdvert.setAdvertiserId(advertiser.getAdvertiserId());
	    
	    
		return true;
		
	}
	/**
	 * This method is to validate the input value when advertiser create the jobAdvert.
     * @param null 
	 * @return boolean.
	 */
	private boolean validateEntryInput(){
	
		if (validateCommon()==false)
			return false;
		
		return true;
		
	}
	/**
	 * This method is to validate the input value when advertiser update the jobAdvert.
	 * @param null 
	 * @return boolean.
	 */
	private boolean validateUpdateInput(){
		if (validateCommon()==false)
			return false;
		
		return true;	
	}
	/**
	 * This method is to save the jobAdvert to properties file.
	 * validate input, if new call <code>validateEntryInput()</code>,
	 * validate input, if update call <code>validateUpdateInput()</code>.
	 * Call job advert controller {@link sg.edu.nus.iss.team2s.jass.control.JobAdvertController}, to create new or update exist.
	 * Pass job advert to job advert controller
	 * Show the result in the results.
	 * 
     * @param null 
	 * @return void
	 * @exception DAOException due to save to property file
	 * 
	 */
	private void save(){
		
		try {
			boolean validated;
			
			 if (isNew){
				 //Add new
				 validated=validateEntryInput();
			 }
			 else{
				 validated=validateUpdateInput();
			 }

			if(validated){
				
				if (isNew){
				JobAdvertController.addJobAdvertEntry(jobAdvert);
				cmd_save.setVisible(false);
				}
				else{
				JobAdvertController.updateJobAdvert(jobAdvert);	
				}
				
				lbl_result.setText("save job successfully!");	
			    }
			
			else{
				lbl_result.setText("Entry validation failed!");	
				CommonUtility.showDialog(this,  "Job Advert Save Error","Error:Can not save job advert, Please check your input!");
			}
		} catch (Exception e) {
			e.printStackTrace();
			CommonUtility.showDialog(this, "Job Advert Save Error",e.getMessage());
		}
	}
	/**
	 * This method is to delete the specified jobAdvert from properties file.
	 * Update job advert object status
	 * Call job advert controller {@link sg.edu.nus.iss.team2s.jass.control.JobAdvertController}, to create new or update exist.
	 * Pass job advert to job advert controller
	 * Show the result in the results.
	 * @param null 
	 * @return void
	 * @exception DAOException due to save to property file
	 */
	private void delete(){
		
		try {
			
			JobAdvertController.deleteJobAdvert(jobAdvert);
			cmd_delete.setVisible(false);
			cmd_withdraw.setVisible(false);
			cmd_save.setVisible(false);
			lbl_result.setText("Delete job successfully!");	
		}catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			CommonUtility.showDialog(this, "Job Advert Delete Error",e.getMessage());
		}
		
	}
	/**
	 * This method is to withdraw the specified jobAdvert.
	 * Update job advert object status
	 * Call job advert controller {@link sg.edu.nus.iss.team2s.jass.control.JobAdvertController}, to create new or update exist.
	 * Pass job advert to job advert controller
	 * Show the result in the results.
	 * @param null 
	 * @return void
	 * @exception DAOException due to save to property file
	 */
	private void withdraw(){
		//update job advert object status
		//call job advert controller, to create new or update exist.
		//pass job advert to jobavert controller
		//show the result in the results.
		//status:
		try {
			JobAdvertController.withdrawJobAdvert(jobAdvert);
			cmd_delete.setVisible(false);
			cmd_withdraw.setVisible(false);
			cmd_save.setVisible(false);
			
			lbl_result.setText("Withdraw job successfully!");
		}catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			CommonUtility.showDialog(this, "Job Advert Delete Error",e.getMessage());
		}
		
	}
	/**
	 * This method is to close the jobAdvertDetailScreen and show the main screen.
	 * 
	 * @param null 
	 * @return void
	 */
	private void cancel(){
		//close the detail screen
		
		this.setVisible(false);
		this.dispose();
	}
	/**
	 * This method is to show JobAdvertResponseScreen for application.
	 * Call job advert controller {@link sg.edu.nus.iss.team2s.jass.control.JobAdvertController) to show JobResponsescreen.
	 * Pass job advert to job advert controller
	 * Show the JobResponsescreen {@link sg.edu.nus.iss.team2s.jass.gui.JobAdvertResponseScreen).
	 * @param null 
	 * @return void
	 * @exception DAOException due to save to property file
	 *
	 */
	private void applyJob(){
		//pass job advert object to job advert controller
		//job advert show job response screen
		//JobAdvertController.apply(this.jobAdvert);
		
		try {
			JobAdvertController.apply(jobAdvert, this);
		
			this.setVisible(false);
			this.dispose();
			
		}catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			CommonUtility.showDialog(this, "Job Advert Apply Error",e.getMessage());
		}
	}
	
}  //  @jve:decl-index=0:visual-constraint="27,-1"
